﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyTruongHoc_App
{
    public partial class QuanLyGiaoVien : Form
    {
        public QuanLyGiaoVien()
        {
            InitializeComponent();
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Format = "dd/MM/yyyy";
            grc_NgaySinh.DefaultCellStyle = style;
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_GIAO_VIEN);
            // user co quyen view
            //tscb_User.Enabled = ((pow & PConst.POW_VIEW) > 0);
            // user co quyen them
            tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);
            // user co quyen edit
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);
            // user co delete
            tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void refreshData()
        {
            if (this.IsDisposed) return;

            view_GiaoVienTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            view_GiaoVienTableAdapter.ClearBeforeFill = true;
            this.view_GiaoVienTableAdapter.FillByFlag(this.qLTH.view_GiaoVien, PConst.FLAG_NONE);
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditGiaoVien frm_AddMH = new AddEditGiaoVien();
            frm_AddMH.MdiParent = this.MdiParent;
            frm_AddMH.Show();
            frm_AddMH.Disposed += new EventHandler(frm_AddMH_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            int i = int.Parse(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            AddEditGiaoVien frm_EditMH = new AddEditGiaoVien();
            frm_EditMH.MdiParent = this.MdiParent;
            frm_EditMH.Show();
            frm_EditMH.setEditInfo(i);
            frm_EditMH.Disposed += new EventHandler(frm_AddMH_Disposed);
        }

        private void frm_AddMH_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void fillSTT()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_MaGV.Name].Value = PublicLib.formatGVId(
                    int.Parse(dg_DanhSach.Rows[i].Cells[grc_id.Name].Value.ToString()));

                String tmp = "Nam";
                if (((int)dg_DanhSach.Rows[i].Cells[grc_GioiTinh_DB.Name].Value) == 1)
                    tmp = "Nữ";

                dg_DanhSach.Rows[i].Cells[grc_GioiTinh_Disp.Name].Value = tmp;
            }
        }

        private void dg_DanhSach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == cl_MaGV.Index)
            {
                switch (cl_MaGV.HeaderCell.SortGlyphDirection)
                {
                    case SortOrder.None:
                    case SortOrder.Descending:
                        dg_DanhSach.Sort(grc_id, ListSortDirection.Ascending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                        break;

                    case SortOrder.Ascending:
                        dg_DanhSach.Sort(grc_id, ListSortDirection.Descending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                        break;
                }
            }
            else
            {
                cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.None;
                if (e.ColumnIndex == grc_GioiTinh_Disp.Index)
                {
                    switch (grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection)
                    {
                        case SortOrder.None:
                        case SortOrder.Descending:
                            dg_DanhSach.Sort(grc_GioiTinh_DB, ListSortDirection.Ascending);
                            grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                            break;

                        case SortOrder.Ascending:
                            dg_DanhSach.Sort(grc_GioiTinh_DB, ListSortDirection.Descending);
                            grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                            break;
                    }
                }
                else
                    grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.None;
            }
        }

        private void dg_DanhSach_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            fillSTT();
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }
            else
                e.Cancel = true;
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }
        }

        private void deleteByID(String _id)
        {
        
            try
            {
                PublicLib.updateField(qLTH.view_GiaoVien.TableName,
                                        qLTH.view_GiaoVien.idColumn.ColumnName,
                                        _id,
                                        qLTH.view_GiaoVien.flagColumn.ColumnName,
                                        PConst.FLAG_DELETE.ToString());
                refreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 15;
            dg_DanhSach.Height = stts_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (tsb_Edit.Enabled)
                bt_Sua_Click(tsb_Edit, new EventArgs());
        }

        private void QuanLyGiaoVien_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
